package org.agile4j.architecture.front.web;

import org.agile4j.architecture.cartmgr.service.ICartService;
import org.agile4j.architecture.cartmgr.vo.CartModel;
import org.agile4j.architecture.cartmgr.vo.CartQueryModel;
import org.agile4j.architecture.goods.service.IGoodsService;
import org.agile4j.architecture.goods.vo.GoodsModel;
import org.agile4j.architecture.goods.vo.GoodsQueryModel;
import org.agile4j.architecture.order.service.IOrderService;
import org.agile4j.common.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class IndexController {

	@Autowired
	private IGoodsService goodsService ;
	
	@Autowired
	private ICartService cartService ;
	
	@Autowired
	private IOrderService orderService ;
	
	@RequestMapping(value="/toIndex", method=RequestMethod.GET)
	public String toIndex(Model model) {
		GoodsQueryModel gqm = new GoodsQueryModel() ;
		gqm.getPage().setRowCount(100);
		Page<GoodsModel> page = goodsService.getByConditionPage(gqm) ; 
		model.addAttribute("page", page) ;
		return "index" ;
	}
	
	@RequestMapping(value="/toGoodsDesc/{goodsUuid}", method=RequestMethod.GET)
	public String toGoodsDesc(Model model, @PathVariable("goodsUuid") Integer goodsUuid) {
		GoodsModel gm = goodsService.getByUuid(goodsUuid) ;
		model.addAttribute("gm", gm) ;
		return "goods/desc";
	}
	
	@RequestMapping(value="/addToCart/{goodsUuid}", method=RequestMethod.GET)
	public String addToCart(Model model,@PathVariable("goodsUuid")int goodsUuid, @CookieValue("MyLogin")String myLogin) {
		int customerUuid = Integer.parseInt(myLogin.split(",")[0]) ;
		
		CartModel cm = new CartModel() ;
		cm.setBuyNum(1);
		cm.setCustomerUuid(customerUuid);
		cm.setGoodsUuid(goodsUuid);
		cartService.create(cm);
		///////////
		CartQueryModel cqm = new CartQueryModel() ;
		cqm.getPage().setRowCount(1000);
		cqm.setCustomerUuid(customerUuid);
		
		Page<CartModel> page = cartService.getByConditionPage(cqm);
		model.addAttribute("page", page) ;
		return "cart/myCart" ;
	}

	@RequestMapping(value="/toCart", method=RequestMethod.GET)
	public String toCart(Model model,@CookieValue("MyLogin")String myLogin) {
		int customerUuid = Integer.parseInt(myLogin.split(",")[0]) ;
		
		CartQueryModel cqm = new CartQueryModel();
		cqm.getPage().setNowPage(1000);
		cqm.setCustomerUuid(customerUuid);
		
		Page<CartModel> page = cartService.getByConditionPage(cqm) ;
		model.addAttribute("page", page) ;
		return "cart/myCart" ;
	}
	
	@RequestMapping(value="/order",method=RequestMethod.GET)
	public String order(@CookieValue("MyLogin")String myLogin){
		int customerUuid = Integer.parseInt( myLogin.split(",")[0]);
		orderService.order(customerUuid);
		return "success";
	}
}
